<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">

          <a-col :md="5" :sm="24">
            <a-form-item label="申请人">
              <a-input v-model="queryParam.creatorName" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="5" :sm="24">
            <a-form-item label="申请金额">
              <a-input v-model="queryParam.applyAmount" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="10" :sm="24">
            <a-form-item label="申请日期">
              <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="24">
            <a-space>
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="handleApply">经费申请</a-button>
            </a-space>

          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'Fund:delete'"><a-icon type="delete" />批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <s-table ref="table" size="default" :scroll="{x:1000}" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :columns="columns" :data="loadDataList" @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handlePreview(record)">查看</a>
        <a @click="handleEdit(record)" v-has="'Fund:edit'" style="margin-left:10px">编辑</a>
        <a-popconfirm v-has="'Fund:delete'" style="margin-left:10px" title="确定删除?" @confirm="() => handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
        <a style="margin-left:10px" @click="handleDeal(record)" v-if="record.dealFlag">审批</a>
      </span>
    </s-table>

    <!-- 申请弹窗 -->
    <FundApplyModal ref="FundApplyModal" @ok="searchQuery"></FundApplyModal>
     <!-- 审批 -->
    <Approval
      v-model="shenpi"
      :bizId="mdl.id"
      :businessUrl="url.add"
      detailComponent="FundApplyForm"
      @ok="searchQuery"
    ></Approval>
    <!-- 查看 -->
    <CheckModal :bizId="detail.id" bizComponent="/funds/modules/FundApplyForm" v-model="detail.visible" ref="CheckModal"></CheckModal>
  
  </a-card>
</template>

<script>
import STable from '@/components/table/'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import XImport from '@/components/extends/XImport.vue'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import AInput from 'ant-design-vue/es/input/Input'
import moment from 'moment'
import {merge} from 'lodash'
import { getFundApply ,setFundApply,delFundApply,delBatchFundApply,getFundApplyById} from '@/api/api'
import {getAction,postAction, deleteAction } from '@/api/manage'
import FundApplyModal from './modules/FundApplyModal'
import JDate from '@/components/jeecg/JDate.vue'
import Approval from '@/components/Approval/Approval'
import CheckModal from '@/components/extends/CheckModel'
export default {
  name: 'TableList',
  components: {
    CheckModal,
    JDate,
    AInput,
    ATextarea,
    STable,
    XImport,
    Approval,
    XSelectUserByDep,
    FundApplyModal
  },
  data() {
    return {
      detail:{},
      visibleImport:false,
      toggleSearchStatus:false,
      shenpi:false,
      form: null,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        
      },
      url:{
        list:'/fundApply/list',
        add:'/fundApply/add',
        exportXlsUrl:'',
        deleteBatch:'/fundApply/deleteBatch'
      },
      // 表头
      columns: [
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'realname',
          width:180,
        },
        {
          title: '申请金额',
          align: 'center',
          dataIndex: 'applyAmount',
          width:180,
        },
        {
          title: '申请理由',
          align: 'center',
          dataIndex: 'reason',
          ellipsis: true,
          width:300,
        },
        {
          title: '申请时间',
          align: 'center',
          dataIndex: 'createTime',
          width:200,
        },
        {
          title: '审批状态',
          align: 'center',
          dataIndex: 'actStatus_dictText',
          width:100,
        },
        {
          title: '当前审批人',
          align: 'center',
          dataIndex: 'waitHandler',
          width:150,
        },
        {
          title: '操作',
          align: 'center',
          fixed:"right",
          width: 220,
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadDataList: parameter => {
        return getFundApply(Object.assign(parameter, this.queryParam)).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
          
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created(){
    this.searchQuery()
  },
  methods: {
    handleAdd(){
      this.$refs.FundApplyModal.title = '添加'
      this.$refs.FundApplyModal.add();
    },
    handleEdit(record){
      this.$refs.FundApplyModal.title = '编辑'

      this.$refs.FundApplyModal.title = '编辑'
      this.$refs.FundApplyModal.edit(record);
    },
    handlePreview(record){
      // this.$refs.FundApplyModal.review(record);
      this.detail = record
      this.$set(this.detail,'visible',true)
    },
    handleToggleSearch(){
      this.toggleSearchStatus = !this.toggleSearchStatus;
    },
    // 申请使用
    handleApply(record){
      this.$refs.FundApplyModal.apply({
        applierId: this.$store.getters.userInfo.id,
        realname: this.$store.getters.userInfo.realname,
      });
    },
    handleDelete(id){
      delFundApply({id}).then(res =>{
          if(res.success){
            this.$message.success('删除成功');
            this.searchQuery()
          }else{
            this.$message.error(res.message)
          }
      })
    },
    /**
     * 处理
     */
    handleDeal(record) {
      this.mdl = Object.assign({}, record)
      this.shenpi = true
    },
    batchDel: function () {
      if(!this.url.deleteBatch){
        this.$message.error("请设置api.leteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
